package org.example.controller.assets;

import lombok.extern.slf4j.Slf4j;
import org.example.common.ApiResponse;
import org.example.entity.Organization;
import org.example.service.impl.OrganizationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Description
 * @Author 魏锴
 * @Date 2024/1/12
 */
@Slf4j
@Controller
@ResponseBody
@RequestMapping("/asset/org")
public class organizationController {
    @Autowired
    OrganizationServiceImpl organizationService;

    @PostMapping("/create")
    public ApiResponse insertOrganization(Organization organizationVO) {
        try {
            if (organizationService.insertOrganization(organizationVO)) return ApiResponse.ok("创建成功");
            else return ApiResponse.failure("创建失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("创建失败");
        }
    }

    @PostMapping("/update")
    public ApiResponse updateOrganization(Organization organizationVO) {
        try {
            if (organizationService.updateOrganization(organizationVO)) return ApiResponse.ok("修改成功");
            else return ApiResponse.failure("修改失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("修改失败");
        }
    }

    @PostMapping("/delete/{id}")
    public ApiResponse deleteOrganization(@PathVariable String id) {
        try {
            if (organizationService.deleteOrganization(Integer.parseInt(id))) return ApiResponse.ok("删除成功");
            else return ApiResponse.failure("删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("删除失败");
        }
    }

    @GetMapping("/list")
    public ApiResponse getOrganization(String father_name, String org_name) {
        //获取组织信息树
        try {
            System.out.println("father_name:" + father_name + ", org_name:" + org_name);
            return ApiResponse.ok(organizationService.getOrganizationList(father_name, org_name));
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("获取失败");
        }
    }
}
